package com.xdtech.base.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.base.dao.DistrictDao;
import com.xdtech.base.model.District;
import com.xdtech.base.service.DistrictService;
import com.xdtech.base.vo.DistrictItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-02 14:02:25
 * @since 1.0
 * @see
 */
@Service
public class DistrictServiceImpl implements DistrictService {
	private Log log = LogFactory.getLog(DistrictServiceImpl.class);
	@Autowired
	private DistrictDao districtDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param entity
	 */
	public void save(District entity) {
		districtDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<District> entities) {
		districtDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param entity
	 */
	public void delete(District entity) {
		districtDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		districtDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param id
	 * @return
	 */
	public District get(Long id) {
		return districtDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @return
	 */
	public List<District> getAll() {
		return districtDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> districts = null;
		long rows = 0;
		if (pg!=null) {
			Page<District> page = districtDao.findPage(pg,"from District order by createTime desc", values);
			districts = BeanUtils.copyListProperties(
					DistrictItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<District> districtList = districtDao.find("from District order by id desc", values);
			districts = BeanUtils.copyListProperties(
					DistrictItem.class, districtList);
			rows = districts.size();
		}
		results.put("total", rows);
		results.put("rows", districts);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateDistrict(DistrictItem item) {
		District district = null;
		if (item.getId()==null) {
			district = new District();
		}else {
			district = districtDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(district, item);
		districtDao.save(district);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param districtId
	 * @return
	 */
	public DistrictItem loadDistrictItem(Long districtId) {
		District district = districtDao.get(districtId);
		DistrictItem districtItem = new DistrictItem();
		BeanUtils.copyProperties(districtItem, district);
		return districtItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteDistrictInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteDistrictInfo(List<Long> districtIds) {
		for (Long id : districtIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:25
	 * @modified by
	 * @return
	 */
	@Override
	public List<DistrictItem> loadItems() {
		List<District> districts = getAll();
		return DistrictItem.createItems(districts);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-2下午9:32:17
	 * @modified by
	 * @param cityId
	 * @return
	 */
	@Override
	public List<District> loadDistrictByCityId(String cityId) {
		List<District> districts = null;
		if (StringUtils.isBlank(cityId)) {
			districts = getAll();
		}else {
			districts = districtDao.findBy("cityId", Long.valueOf(cityId));
		}
		return districts;
	}

}
